<?php

namespace App\Http\Middleware;

use Closure;
use App\Library\JsonData;
use Illuminate\Support\Facades\DB;

class UserAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $token  = trim($request->header('token'));
        if($token){
            $user_id = DB::table('co_user')->where('api_token',$token)->value('id');
            if(!$user_id){
                return JsonData::error("用户未登录",'',601);
            }
        }else{
            return JsonData::error("用户未登录",'',601);
        }
        return $next($request);
    }
}
